<template>
  <a-card>
    <div style="margin: auto; width: 100%; height: 80vh">
      <FlowDesignTCD
        v-if="isshow"
        v-model:edgeModel="edgeModel"
        v-model:nodeModel="nodeModel"
        :flowData="g6data"
        @save="save"
      ></FlowDesignTCD>
    </div>
  </a-card>
</template>
<script>
  import { Add, Get, Update } from '@/api/flow/api'

  export default {
    data() {
      return {
        isshow: false,
        nodeModel: {
          label: '',
          forms: '',
          handleNode: '',
          strategy: {
            processorType: '1', // 策略处理人
            defaultHandlerName: '', // 默认处理人姓名
            defaultHandlerIds: '', // 默认处理人ids
          },
          targetKeys: [], // 按钮穿梭框数据
        },
        edgeModel: {
          label: '',
          sql: '',
          organizeData: [
            {
              left: '',
              fieldType: '',
              equal: '0',
              value: '0',
              right: '',
              andor: '',
              organizeName: '',
            },
          ],
        },
        g6data: {}, // 保存的json数据包
      }
    },
    created() {
      var that = this
      if (that.$route.query.id != undefined) {
        Get({ id: that.$route.query.id }).then((res) => {
          this.g6data = JSON.parse(res.content.designerJSON)
          // console.log(this.g6data)
          this.isshow = true
        })
      } else {
        this.isshow = true
        this.g6data = {
          flowInfo: {},
          g6data: {
            database: [],
          },
        }
      }
    },
    methods: {
      save(data, info) {
        var that = this
        console.log('save', data, info)
        var jsonData = {
          DictId: info.dictId,
          Title: info.name,
          DesignerJSON: JSON.stringify({
            g6data: data,
            flowInfo: info,
          }),
          Note: info.note,
        }
        if (that.$route.query.id !== undefined) {
          Update({ id: that.$route.query.id }, jsonData)
            .then((res) => {
              if (res.isSuccess) {
                that.$message.success('恭喜你，修改成功！')
              } else {
                that.$message.error('操作失败，请刷新重试')
              }
            })
            .catch((err) => {
              console.log(err)
              this.$message.error(err.errormessage)
            })
            .finally(() => {
              this.isloading = false
            })
        } else {
          Add(jsonData).then((res) => {
            if (res.isSuccess) {
              console.log('添加成功！', res)
            }
          })
        }
      },
    },
  }
</script>
